//
//  FrameY.m
//  自定义collectionView
//
//  Created by apple on 15-1-12.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "FrameY.h"

@implementation FrameY
@synthesize delegate;
- (instancetype)initWithArray:(NSArray *)array
{
    self = [super init];
    if (self) {
        
        ztxArray  =[array mutableCopy];
        ZTXFlowLayout * flowLayout = [[ZTXFlowLayout alloc] init];
        flowLayout.delegate = self;
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
        
        
        ztxCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 200, 300) collectionViewLayout:flowLayout];
        [ztxCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell0090"];
        ztxCollectionView.delegate = self;
        ztxCollectionView.dataSource = self;
//        ztxCollectionView.backgroundColor = [UIColor redColor];
        ztxCollectionView.contentSize =  CGSizeMake(100, 100);
        [self addSubview:ztxCollectionView];
    }
    return self;
}

#pragma  mark -  MyFlowLayoutDelegate
-(void)LastZTXFlowLayoutFrameY:(float)orignY
{  //里面cell的高度不要超过tabCellCollectionView.frame 就不会滚动
    ztxCollectionView.frame = CGRectMake(0, 0, 240, orignY+20+10);//本身的高度点+自身的高度+与底部间隔
    frameLayoutY = orignY+20;
    [delegate LastFrameY:frameLayoutY];
    
}

#pragma mark -  UICollectionViewDataSource && UICollectionViewDelegate

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return ztxArray.count;
}

//每个UICollectionView展示的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellIdentifier = @"cell0090";
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    [cell sizeToFit];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:cell.bounds];
    titleLabel.text = ztxArray[indexPath.row];
    titleLabel.textAlignment = NSTextAlignmentCenter;
//    titleLabel.font = [UIFont systemFontOfSize:10];
    titleLabel.layer.borderWidth = 1;
    titleLabel.layer.borderColor = [UIColor blueColor].CGColor;
    titleLabel.layer.cornerRadius = 5;
    [cell addSubview:titleLabel];
    cell.backgroundColor = [UIColor clearColor];
    
    return cell;
}

//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *titleC = ztxArray[indexPath.row];
    NSDictionary *attDic = @{NSFontAttributeName: [UIFont systemFontOfSize:11]};
    CGSize fontSize = [titleC[@"name"] sizeWithAttributes:attDic];
    return CGSizeMake(fontSize.width + 10, 20);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    UIEdgeInsets top = {10, 10, 10, 10};//上 右
    return top;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%zd", indexPath.row);
}


@end
